<?php
/*\
 *	Architarra
 *	Gestionale per l'archiviazione digitale dei documenti
 *	-----------------------------------------------------
 *	by TotalInnovation s.r.l.
\*/	require_once("../../oh.framework/oh.framework.php");
	require_once("barcode-ean.php");

define("ARC_DEBUG",false);

#database conf
$oh['db']['host'] 		= "localhost";
$oh['db']['name'] 		= "db_architarra";
$oh['db']['username'] 	= "root";
$oh['db']['password'] 	= "root";

$menu = array(
	"home" 		=> "[Home] 				?page=home",
	"sistema" 	=> "[Sistema] 			?page=sistema",
	"acquisisci"=> "[Ascquisisci]		?page=acquisisci",
	"pratica"	=> "[Pratica]			?page=pratica",
	"faldone"	=> "[Faldone]			?page=faldone",
);

$page = oh_get('page',array_keys($menu));

arc_header();
switch($page) {
	case "home":
		arc_home();
		break;
	case "acquisisci":
		arc_acquisisci();
		break;
	case "no":	
		break;
	case "pratica":
		arc_pratica();
		break;
	case "faldone":
		arc_faldone();
		break;
	default:
		arc_404();
}
arc_footer();

function arc_home() {
	oh("databaseform")->draw("pratica");
}

function arc_header() {
	global $menu;
	?>
<html>
<head>
<link rel="stylesheet" href="style.css" type="text/css" media="screen" />
<script type="text/javascript" src="jquery-1.4.2.min.js"></script>
</head>
<body>
<div id="title">
	DIGIT (Architarra v0.0.1) - Comune di Castelvetrano - Ufficio Ediliezia Privata
	<span class="right">
		<a href="database.php" target="_blank">DATABASE</a>
	</span>
</div>
<div id="dolphincontainer">
	<div id="dolphinnav">
		<ul>
			<?php
			foreach($menu as $k=>$v) {
				$item = oh_menu_item($k,$v);
				if (isset($_GET['page'])&&$_GET['page']==$item['key']) {
					$current = 'class="current"';
				} else {
					$current = "";	
				}
				echo '<li><a '.$current.' href="'.$item['url'].'"><span>'.$item['label'].'</span></a></li>'."\n";
			}
			?>		
		</ul>
	</div>
	<div id="dolphininner">
		Hellow
	</div>
</div>	
<div id="content">
	<?php
}

function arc_footer() {
	?>
</div>
<div id="status">
</div>
</body>
</html>
	<?php
}

function arc_caricadati() {
	
#	oh("simpleform")->
}

function arc_acquisisci() {
	echo '<div class="center">';
	if (ARC_DEBUG) {
		$code = barcode_new(1,1,rand(1,999999));
		oh_dump(barcode_explode($code));
	} else {
		$code = "";
	}
	oh("simpleform")
		->open("f","?page=pratica&do=acquisisci")
		->label("Inserisci un codice a barre")->br()
		->text("code", $code )
		->submit("Carica")
		->close()
	;
	?>
<script type="text/javascript">
jQuery(document).ready(function(){
	document.f.code.focus();
});
</script>
	<?php
	echo '</div>';
}

function arc_faldone() {
	$code = $_GET['code'];
	oh("databaseform")->draw("carpettone");
}

function arc_pratica() {
	if (isset($_POST['code'])) {
		$code = $_POST['code'];
		if (barcode_have_layer($code,"2")) {
			oh_redirect("?page=faldone&code=$code&do=acquisisci");
		}
		if (barcode_is_valid($code))	{
			if (barcode_have_identity($code,"909")) {			
				$tcode 		= barcode_explode($code);
				$pratica 	= $tcode['load']%100;
				$faldone 	= oh_intdiv($tcode['load'],100);
			
				echo '<div class="info">pratica: '.$pratica.' - faldone: '.$faldone.'</div>';
			
				if (oh("mysql")->query("INSERT INTO pratica(codice) VALUES ('$code')")) {				
				} else {
					oh_dump(mysql_error());
				}
			} else {
				oh_redirect("?page=acquisisci&msg=1001");
			}
		} else {
			echo "bo";
		}
	}
	oh("databaseform")->draw("pratica");
}

function arc_404() {
	echo "pagina non trovata";
}

function oh_intdiv($x, $y) {
    return ($x - ($x % $y)) / $y;
}

function oh_redirect($url) {
	header("Location: $url");
	ob_end_flush();
	die();
}
